package info.rongtong.utils;

import info.rongtong.domain.Article;

import java.io.File;

import javax.smartcardio.ATR;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryTermScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;



public class LuceneUtil {

	private LuceneUtil(){};
	
	private static Directory directory = null;
	
	private static IndexWriter indexWriter = null;
	
	private static IKAnalyzer ikAnalyzer = null;
	
	//初始化
	public static void init(String path){
		try {
			//存放的目标
			directory = FSDirectory.open(new File(path));
			//分词器
			ikAnalyzer = new IKAnalyzer(true);
			//创建写入
			indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_42,LuceneUtil.ikAnalyzer));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//销毁
	public static void destory(){
		try{
		if(indexWriter!=null)
			indexWriter.close();
		if(directory!=null){
			directory.close();
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	//高亮器
	public static String highlighterGremark(Query query,int wordnum,String value){
		Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<span color='red'>","</span>"),new QueryTermScorer(query));
		highlighter.setTextFragmenter(new SimpleFragmenter(wordnum));
		String text = null;
		try {
			text = highlighter.getBestFragment(LuceneUtil.ikAnalyzer, null, value);
			if (text == null) {
				text = value;
				int size = text.length() < wordnum ? text.length() : wordnum;
				text = text.substring(0, size);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return text;
	}
	
	//转换文档
	public static Document ArticleToDocument(Article article){
		Document doc = new Document();
		//Field.Store指定当前字段的索引方式。  
        //Field.Store.NO：不要在索引中存储的字段的值。  
        //Field.Store.YES:在索引存储原始字段值。  
        //TextField.TYPE_STORED:索引，分词，存储。  
        //TextField.TYPE_NOT_STORED:索引，分词，不存储。  
		doc.add(new IntField("id", article.getAid(), Field.Store.YES));
		doc.add(new Field("title", article.getAtitle(), TextField.TYPE_STORED));
		doc.add(new Field("autohr",article.getAuthor(),TextField.TYPE_STORED));
		doc.add(new Field("alabel", article.getAlabel(), TextField.TYPE_STORED));
		doc.add(new Field("content",article.getAcontent(),TextField.TYPE_STORED));
		return doc;
	}
	
	//将文档转换成对象
	
	public static Article DocumentToArticle(Document doc){
		Article article = new Article();
		article.setAid(Integer.parseInt(doc.get("id")));
		article.setAtitle(doc.get("title"));
		article.setAuthor(doc.get("author"));
		article.setAlabel(doc.get("alabel"));
		article.setAcontent(doc.get("content"));
		return article;
	}
	
	
	
	public static Directory getDirectory() {
		return directory;
	}

	public static void setDirectory(Directory directory) {
		LuceneUtil.directory = directory;
	}

	public static IndexWriter getIndexWriter() {
		return indexWriter;
	}

	public static void setIndexWriter(IndexWriter indexWriter) {
		LuceneUtil.indexWriter = indexWriter;
	}

	public static IKAnalyzer getIkAnalyzer() {
		return ikAnalyzer;
	}

	public static void setIkAnalyzer(IKAnalyzer ikAnalyzer) {
		LuceneUtil.ikAnalyzer = ikAnalyzer;
	}
	
	
	
	
	
}
